package com.cpi.cornpp.purchaseorder.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.biweek.exception.BiWeekException;
import com.cpi.cornpp.biweeks.services.BiWeekPeriodVersionService;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.corntypes.services.CornTypesService;
import com.cpi.cornpp.plants.services.PlantService;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.form.PurchaseOrderVersionForm;
import com.cpi.cornpp.purchaseorder.service.PurchaseOrderCostVersionService;
import com.cpi.cornpp.purchaseorder.service.PurchaseOrderVersionService;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportView;

public class ShowEditPurchaseOrderVersionAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";//$NON-NLS-1$
	private static final String SHOW_PURCHASE_ORDER_VERSION_LIST_FORWARD = "showPurchaseOrderVersionList";//$NON-NLS-1$
	private static final String FAILURE_FORWARD = "error";//$NON-NLS-1$
	private static final String PURCHASE_ORDER_VERSION_ID = "purchaseOrderVersionId";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException{
		String versionId = null;
		PurchaseOrderVersionForm povForm = null;
		PurchaseOrderVersionView povView = null;
		TransportView transportView = null;
		BiWeekPeriodVersionService bpvService = null;
		PlantService plantService = null;
		PurchaseOrderVersionService povService = null;
		PurchaseOrderCostVersionService pcvService = null;
		TransportService transportService = null;
		CornTypesService cornTypesService = null;
		
		List cornTypes = null;
		List plantsView = null;
		List versionPurchaseDatesView = null;
		List transportCostElementsView = null;
		HttpSession session = null;
		ActionForward forward = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		session = request.getSession(false);
		versionId = (String) session.getAttribute("versionId");
		
		try{
			
			if ((form != null) && (versionId != null)){
				povForm = (PurchaseOrderVersionForm) form;
				
				if (! (StringUtils.isNotEmpty(povForm.getPurchaseOrderVersionId()) && StringUtils.isNumeric(povForm.getPurchaseOrderVersionId()) )){
					povForm.setPurchaseOrderVersionId((String)session.getAttribute(PURCHASE_ORDER_VERSION_ID));
				}else{
					session.setAttribute(PURCHASE_ORDER_VERSION_ID, povForm.getPurchaseOrderVersionId());
				}
				
				povService = new PurchaseOrderVersionService();
				povView = povService.getPurchaseOrderVersion(new Integer(povForm.getPurchaseOrderVersionId()));
				
				if (povView != null){
					cornTypesService = new CornTypesService();
					bpvService = new BiWeekPeriodVersionService();
					pcvService = new PurchaseOrderCostVersionService();
					plantService = new PlantService();
					transportService = new TransportService();
					
					povView.setPurchaseOrderCostVersions(pcvService.getPurchaseOrderCostVersionByPovId(povView.getPurchaseOrderVersionId()));
					
					if( povView.getTransportId() != null){
						transportView = transportService.getTransport(povView.getTransportId().intValue());
						transportCostElementsView = transportService.getTransportCostElements(povView.getTransportId().intValue());
						transportView.setTransportCostElements(transportCostElementsView);
					}
					
					versionPurchaseDatesView = bpvService.getBiWeekPeriodVersionsByVersion(new Integer(versionId));
					plantsView = plantService.list(0, true);
					
					cornTypes = cornTypesService.getListCornTypes();
					
					request.setAttribute("purchaseOrderVersion", povView);
					request.setAttribute("transportView", transportView);
					request.setAttribute("versionPurchaseDates", versionPurchaseDatesView);
					request.setAttribute("plants", plantsView);
					request.setAttribute("cornTypes", cornTypes);
					
					forward = mapping.findForward(SUCCESS_FORWARD);
				} else {
					forward = mapping.findForward(SHOW_PURCHASE_ORDER_VERSION_LIST_FORWARD);
				}
			} else {
				forward = mapping.findForward(SHOW_PURCHASE_ORDER_VERSION_LIST_FORWARD);
			}
			
		} catch(BiWeekException bwe){
			PurchaseProgramException ppe = new PurchaseProgramException(bwe, PurchaseProgramException.LAYER_ACTION, PurchaseProgramException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, bwe);
			throw ppe;
		} catch(Exception e){
			PurchaseProgramException ppe=null;
			e.printStackTrace(System.out);
			ppe= new PurchaseProgramException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			throw ppe;
		}
		
		return forward;
	}
}